<?php
/**
 * Created by PhpStorm.
 * User: xuanbang.wen
 * Date: 2020/2/18
 * Time: 10:08
 */

namespace App\Api\Model;

use App\Service\ExternalAPI\VSApi;
use App\Service\Mailer as MailerService;
use App\Service\Sms as SmsService;
use EchoPHP\Facades\Db;
use EchoPHP\Facades\Log;
use Exception;
use PHPMailer\PHPMailer\Exception as PHPMailerException;

class Interview extends Model {

    /**
     * 获取面试详细信息
     * @param $jobseekerid_out
     * @return mixed
     */
    public function getInterviewDetail($jobseekerid_out) {
        $sql = 'select a.jobseekerid,a.jobseekerid_out,a.cname,a.starttime,b.caseshowname,b.content as casedesc,c.subshowname,c.introduce as subdesc,d.mobilephone as contactormp from RoomJobseeker a LEFT JOIN SCase b ON a.caseid=b.caseid LEFT JOIN CustomerSub c on a.subid=c.subid   LEFT JOIN User d on a.creator=d.userid WHERE jobseekerid_out=?';
        $var = [$jobseekerid_out];

        return Db::connect()->selectOne($sql, $var);
    }

    /**  面试官提醒
     * @param $interviewerid_out
     * @param int $remindtype //1:安排面试时提醒 2:面试开始30分前提醒 3:手动触发
     * @return bool
     * @throws Exception
     */
    public function remindInterviewer($interviewerid_out, $remindtype = 1) {
        $sql = 'select roomid,subid,projectid,creator,cname,mobilephone,email,starttime,endtime,authorizecode_out from RoomInterviewer where interviewerid_out = ? and status = ? and endtime> NOW()';
        $interviewerData = Db::selectOne($sql, [$interviewerid_out, '03']);
        if (!$interviewerData) {
            if ($remindtype == 1) {
                sleep(2);
                $interviewerData = Db::selectOne($sql, [$interviewerid_out, '03']);
                if (!$interviewerData) {
                    $this->sendResult(-9);
                }
            }
        }

        $customerSubInfo = $this->getCustomerSub($interviewerData['subid']);
        $projectInfo = $this->getProject($interviewerData['projectid']);

        $subName = $customerSubInfo['subname'];
        $projectName = $projectInfo['projectname'];

        $roomid = $interviewerData['roomid'];
        $cname = $interviewerData['cname'];
        $mobile = $interviewerData['mobilephone'];
        $email = $interviewerData['email'];
        $authorizeCode = $interviewerData['authorizecode_out'];

        $options = config('extraapi.vsapi', []);
        $url = $options['interview_url'];  //jobseeker_url

        $roomInfo = $this->getRoom($roomid);
        $roomName = $roomInfo['roomname'];
        $isTemp = $roomInfo['istemp'];

        $startTime = date('Y-m-d H:i', strtotime($interviewerData['starttime']));
        $endTime = date('H:i', strtotime($interviewerData['endtime']));
        $interviewTime = $startTime; // . ' - ' . $endTime;

        $resultUser = $this->getUser($interviewerData['creator']);
        $rpoName = $resultUser['cname'];
        $rpoPhone = $resultUser['mobilephone'];

        $strMsg = $isTemp == 1 ? '[非正式面试]' : '';
        if ($remindtype == 2) {
            $strMsg .= '尊敬的面试官' . $cname . ',您好,邀请您在' . $startTime . '进行的在线面试即将开始,';
        } else {
            $strMsg .= '尊敬的面试官' . $cname . ',您好,邀请您在' . $startTime . '进行在线面试,';
        }
        //$strMsg .= '建议您提前10分钟进入面试间准备。面试时间:' . $interviewTime . ',';
        $strMsg .= '您的面试验证码: ' . $authorizeCode . ' ,移动端请微信搜索小程序“无忧RPO”,';
        $strMsg .= 'PC端请登录 ' . $url . ' ,记得准时参加哦!';
        //$strMsg .= '联系人:' . $rpoName . ',手机:' . $rpoPhone;
        $resultSend = $this->sendMsg($mobile, $strMsg);
        if (!empty($email)) {
            $resultSend2 = $this->sendMailRemindInterviewer($remindtype, $email, $cname, $subName, $projectName, $roomName, $url, $interviewTime, $authorizeCode, $rpoName, $rpoPhone, $isTemp);
        }
    }

    /** 取消面试官 提醒
     * @param $interviewerid_out
     * @return bool
     */
    public function remindCancelInterviewer($interviewerid_out) {
        $sql = 'select roomid,subid,projectid,creator,cname,mobilephone,email,starttime,endtime,authorizecode_out from RoomInterviewer where interviewerid_out = ? and endtime> NOW()';
        $interviewerData = Db::selectOne($sql, [$interviewerid_out]);
        if (!$interviewerData) {
            return false;
        }

        $customerSubInfo = $this->getCustomerSub($interviewerData['subid']);
        $projectInfo = $this->getProject($interviewerData['projectid']);

        $subName = $customerSubInfo['subname'];
        $projectName = $projectInfo['projectname'];

        $roomid = $interviewerData['roomid'];
        $cname = $interviewerData['cname'];
        $mobile = $interviewerData['mobilephone'];
        $email = $interviewerData['email'];

        $roomInfo = $this->getRoom($roomid);
        $roomName = $roomInfo['roomname'];

        $startTime = date('Y-m-d H:i', strtotime($interviewerData['starttime']));
        $endTime = date('H:i', strtotime($interviewerData['endtime']));

        $interviewTime = $startTime; // . ' - ' . $endTime;

        $resultUser = $this->getUser($interviewerData['creator']);
        $rpoName = $resultUser['cname'];
        $rpoPhone = $resultUser['mobilephone'];

        $strMsg = $roomInfo['istemp'] == 1 ? '[非正式面试]' : '';

        $strMsg .= '尊敬的面试官' . $cname . ',您好,邀请您在' . $startTime . '进行的在线面试已取消。';
        //$strMsg .= '联系人:' . $rpoName . ',手机:' . $rpoPhone;

        $resultSend = $this->sendMsg($mobile, $strMsg);
        if (!empty($email)) {
            $resultSend2 = $this->sendMailCancelRemindInterviewer($email, $cname, $subName, $projectName, $roomName, $interviewTime, $rpoName, $rpoPhone, $roomInfo['istemp']);
        }
    }


    /** 求职者提醒
     * @param $jobseekerid_out
     * @param int $remindtype //1:安排面试时提醒 2:面试开始30分前提醒 3:手动触发
     * @return bool
     * @throws Exception
     */
    public function remindJobseeker($jobseekerid_out, $remindtype = 1) {
        $sql = 'select roomid,subid,projectid,caseid,creator,cname,mobilephone,email,starttime,endtime,authorizecode_out from RoomJobseeker where jobseekerid_out = ? and status = ? and endtime> NOW()';
        $jobseekerData = Db::selectOne($sql, [$jobseekerid_out, '03']);
        if (!$jobseekerData) {
            if ($remindtype == 1) {
                sleep(2);
                $jobseekerData = Db::selectOne($sql, [$jobseekerid_out, '03']);
                if (!$jobseekerData) {
                    $this->sendResult(-9);
                }
            }
        }

        $customerSubInfo = $this->getCustomerSub($jobseekerData['subid']);
        $projectInfo = $this->getProject($jobseekerData['projectid']);
        $caseInfo = $this->getCase($jobseekerData['caseid']);

        $subName = $customerSubInfo['subname'];
        $projectName = $projectInfo['projectname'];
        $caseName = $caseInfo['casename'];
        $caseContent = $caseInfo['content'];

        $roomid = $jobseekerData['roomid'];
        $cname = $jobseekerData['cname'];
        $mobile = $jobseekerData['mobilephone'];
        $email = $jobseekerData['email'];
        $authorizeCode = $jobseekerData['authorizecode_out'];

        $options = config('extraapi.vsapi', []);
        $url = $options['interview_url'];  //jobseeker_url

        $roomInfo = $this->getRoom($roomid);
        $roomName = $roomInfo['roomname'];
        $isTemp = $roomInfo['istemp'];

        $startTime = date('Y-m-d H:i', strtotime($jobseekerData['starttime']));
        $endTime = date('H:i', strtotime($jobseekerData['endtime']));
        $interviewTime = $startTime; // . ' - ' . $endTime;

        $resultUser = $this->getUser($jobseekerData['creator']);
        $rpoName = $resultUser['cname'];
        $rpoPhone = $resultUser['mobilephone'];

        if ($remindtype == 2) {
            $strMsg = $cname . ',您好,' . $subName . '邀请你在' . $startTime . '进行的在线面试即将开始,';
        } else {
            $strMsg = $cname . ',您好,' . $subName . '邀请你在' . $startTime . '进行在线面试,';
        }
        //$strMsg .= '建议您提前10分钟进入面试间准备。面试时间:' . $interviewTime . ',';
        $tempStr = $isTemp == 1 ? '[非正式面试]' : '';

        $strMsg .= '您面试的职位为' . $tempStr . $caseName . ',您的面试验证码: ' . $authorizeCode . ' ,';
        $strMsg .= '移动端请微信搜索小程序“无忧RPO”，PC端请登录 ' . $url . ' ,记得准时参加哦!';

        //$strMsg .= '联系人:' . $rpoName . ',手机:' . $rpoPhone;

        $resultSend = $this->sendMsg($mobile, $strMsg);

        if (!empty($email)) {
            $resultSend2 = $this->sendMailRemindJobseeker($remindtype, $email, $cname, $subName, $projectName, $caseName, $caseContent, $roomName, $url, $interviewTime, $authorizeCode, $rpoName, $rpoPhone, $isTemp);
        }
    }


    /** 取消求职者 提醒
     * @param $jobseekerid_out
     * @return bool
     */
    public function remindCancelJobseeker($jobseekerid_out) {
        $sql = 'select roomid,subid,projectid,caseid,creator,cname,mobilephone,email,starttime,endtime,authorizecode_out from RoomJobseeker where jobseekerid_out = ? and endtime> NOW()';
        $jobseekerData = Db::selectOne($sql, [$jobseekerid_out]);
        if (!$jobseekerData || !$jobseekerid_out) {
            return false;
        }

        $customerSubInfo = $this->getCustomerSub($jobseekerData['subid']);
        $projectInfo = $this->getProject($jobseekerData['projectid']);
        $caseInfo = $this->getCase($jobseekerData['caseid']);

        $subName = $customerSubInfo['subname'];
        $projectName = $projectInfo['projectname'];
        $caseName = $caseInfo['casename'];
        $roomid = $jobseekerData['roomid'];
        $cname = $jobseekerData['cname'];
        $mobile = $jobseekerData['mobilephone'];
        $email = $jobseekerData['email'];

        $roomInfo = $this->getRoom($roomid);
        $roomName = $roomInfo['roomname'];

        $startTime = date('Y-m-d H:i', strtotime($jobseekerData['starttime']));
        $endTime = date('H:i', strtotime($jobseekerData['endtime']));
        $interviewTime = $startTime; //. ' - ' . $endTime;

        $resultUser = $this->getUser($jobseekerData['creator']);
        $rpoName = $resultUser['cname'];
        $rpoPhone = $resultUser['mobilephone'];
        $strMsg = $roomInfo['istemp'] == 1 ? '[非正式面试]' : '';
        $strMsg .= $cname . ',您好,' . $subName . '邀请你在' . $startTime . '进行的在线面试已取消。';
        //$strMsg .= '联系人:' . $rpoName . ',手机:' . $rpoPhone;

        $resultSend = $this->sendMsg($mobile, $strMsg);

        if (!empty($email)) {
            $resultSend2 = $this->sendMailCancelRemindJobseeker($email, $cname, $subName, $projectName, $caseName, $roomName, $interviewTime, $rpoName, $rpoPhone, $roomInfo['istemp']);
        }
    }

    /** 返回结果
     * @param $str
     * @param string $errMessage
     */
    public function sendResult($str, $errMessage = '') {
        //file_put_contents('../../storage/log/web/log.txt', "执行结果：" . $str . '_' . $errMessage . "\r\n\r\n", FILE_APPEND);
        echo $str;
        exit();
    }

    /** 发送提醒短信
     * @param $mobile
     * @param $content
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sendMsg($mobile, $content) {
        if (empty($mobile)) {
            return ['status' => -1, 'message' => '短信发送失败'];
        }
        $sms = new SmsService();
        $channel = config('app.sms.channel.account');

        $result = $sms->sendOne($mobile, $content, $channel);
        //var_dump($content);

        //file_put_contents('../../storage/log/web/log.txt', "短信内容结果：" . $mobile . '_' . $content . "\r\n\r\n", FILE_APPEND);

        if ($result) {
            return ['status' => 0, 'message' => '短信发送成功'];
        }
        return ['status' => -1, 'message' => '短信发送失败'];
    }

    /** 获取房间信息
     * @param $roomid
     * @return array|mixed
     */
    public function getRoom($roomid) {
        $sql = 'select roomname,starttime,endtime,istemp from Room where roomid = ?';
        $result = Db::selectOne($sql, [$roomid]);
        if (!$result) {
            return [];
        }
        return $result;
    }

    /** 获取顾问信息
     * @param $roomid
     * @return array|mixed
     */
    public function getUser($userid) {
        //暂不返回顾问信息
        return ['cname' => '', 'mobilephone' => ''];

        $sql = 'select cname,mobilephone from User where userid = ?';
        $result = Db::selectOne($sql, [$userid]);
        if (!$result) {
            return ['cname' => '', 'mobilephone' => ''];
        }
        return $result;
    }

    /** 获取客户信息
     * @param $roomid
     * @return array|mixed
     */
    public function getCustomerSub($subid) {
        $sql = 'select subname from CustomerSub where subid = ?';
        $result = Db::selectOne($sql, [$subid]);
        if (!$result) {
            return [];
        }
        return $result;
    }

    /** 获取项目信息
     * @param $roomid
     * @return array|mixed
     */
    public function getProject($projectid) {
        $sql = 'select projectname from Project where projectid = ?';
        $result = Db::selectOne($sql, [$projectid]);
        if (!$result) {
            return [];
        }
        return $result;
    }


    /** 获取职位信息
     * @param $roomid
     * @return array|mixed
     */
    public function getCase($caseid) {
        $sql = 'select casename,content from SCase where caseid = ?';
        $result = Db::selectOne($sql, [$caseid]);
        if (!$result) {
            return [];
        }
        return $result;
    }

    /** 更新面试官 信息
     * @param $interviewerid_out
     * @param $noticetype
     * @param $time
     */
    public function updateInterviewInfo($interviewerid_out, $noticetype, $time) {
        $sql = 'update RoomInterviewer set starttime_line = ? WHERE interviewerid_out=? and (starttime_line is null or starttime_line<starttime)';
        $var = [
            $time,
            $interviewerid_out
        ];
        $result = Db::update($sql, $var);
    }


    /** 更新求职者信息
     * @param $jobseekerid_out
     * @param $noticetype
     * @param $time
     * @param $videoplayurl
     */
    public function updateJobseekerInfo($jobseekerid_out, $noticetype, $time) {
        $timeFieldName = $noticetype == 1 ? 'starttime_video' : 'endtime_video';
        $sql = 'update RoomJobseeker set ' . $timeFieldName . ' = ?, time_video=TIMESTAMPDIFF(SECOND, starttime_video,endtime_video) WHERE jobseekerid_out=?';
        $var = [
            $time,
            $jobseekerid_out
        ];
        $result = Db::update($sql, $var);
    }


    /** 更新面试结果
     * @param $jobseekerid_out
     * @param $interviewerid_out
     * @param $summary
     * @param $score
     * @param $comment
     */
    public function updateJobseekerScoreInfo($jobseekerid_out, $interviewerid_out, $summary, $score, $comment) {
        try {
            $sql = 'select jobseekerid,roomid from RoomJobseeker where jobseekerid_out = ?';
            $jobseekerResult = Db::selectOne($sql, [$jobseekerid_out]);

            $sql = 'select roominterviewerid from RoomInterviewer where interviewerid_out = ?';
            $interviewerResult = Db::selectOne($sql, [$interviewerid_out]);

            $sql = 'replace into RoomJobseekerResult(uuid,roomid,jobseekerid,roominterviewerid,summary,score,comment,createdate)
        values(?,?,?,?,?,?,?,NOW())';
            $var = [
                $jobseekerResult['jobseekerid'] . '_' . $interviewerResult['roominterviewerid'],
                $jobseekerResult['roomid'],
                $jobseekerResult['jobseekerid'],
                $interviewerResult['roominterviewerid'],
                mb_substr($summary, 0, 50),
                mb_substr($score, 0, 200),
                $comment
            ];
            $result = Db::insert($sql, $var);
            return $result;
        } catch (Exception $ex) {
            Log::info('视频面试接口 updateJobseekerScoreInfo 错误', $ex->getMessage());
            return false;
        }
    }


    /** 更新面试视频回看结果
     * @param $jobseekerid_out
     * @param $interviewerid_out
     * @param $videoData
     */
    public function updateJobseekerVideoInfo($jobseekerid_out, $videoData) {
        try {
            $sql = 'select jobseekerid,roomid from RoomJobseeker where jobseekerid_out = ?';
            $jobseekerResult = Db::selectOne($sql, [$jobseekerid_out]);
            if (!isset($jobseekerResult['roomid'])) {
                return false;
            }

            $roomId = $jobseekerResult['roomid'];
            $jobseekerId = $jobseekerResult['jobseekerid'];

            $result = Db::connect('jyrpo_pdo')->transaction(function ()
            use ($roomId, $jobseekerId, $videoData) {
                $sql = ' replace into RoomJobseekerVideo(uuid,roomid,jobseekerid,';
                $sql .= ' filename,type,coverurl,mediaurl,createdate,starttime,endtime) ';
                $sql .= ' values(?,?,?,?,?,?,?,?,?,?) ';
                foreach ($videoData as $item) {
                    $var = [
                        $item['fileid'],
                        $roomId,
                        $jobseekerId,
                        mb_substr($item['filename'], 0, 200),
                        mb_substr($item['type'], 0, 20),
                        mb_substr($item['coverurl'], 0, 500),
                        mb_substr($item['mediaurl'], 0, 500),
                        $item['createtime'],
                        $item['starttime'],
                        $item['endtime']
                    ];
                    Db::connect('jyrpo_pdo')->update($sql, $var);
                }
            });
            return true;
        } catch (Exception $ex) {
            Log::info('视频面试接口 updateJobseekerVideoInfo 错误', $ex->getMessage());
            return false;
        }
    }


    /** 验证令牌
     * @param $postData
     * @return int
     */
    public function checkToken($postData) {
        try {
            if (empty($postData) || count($postData) < 3 || empty($postData['token']) || empty($postData['timestamp'])) {
                return 0;  //参数错误
            }
            if (time() - $postData['timestamp'] > 60) {
                return -1;  //时间戳已过期
            }
            $videoService = new VSApi();
            $strToken = $videoService->generateToken($postData);
            if ($strToken != $postData['token']) {
                return -2;  //token验证失败
            }
            return 1;       //token验证失败
        } catch (Exception $ex) {
            Log::info('视频面试接口 checkToken 错误', $ex->getMessage());
            //file_put_contents('../../storage/log/web/log.txt', "执行结果：-9" . $ex->getMessage() . "\r\n\r\n", FILE_APPEND);
            return -9;
        }
    }

    /**
     * Notes:面试官提醒邮件
     * @param $remindtype
     * @param $emails
     * @param $name
     * @param $subName
     * @param $projectName
     * @param $roomName
     * @param $url
     * @param $interviewTime
     * @param $authorizeCode
     * @param $rpoName
     * @param $rpoPhone
     * @param $isTemp
     * @return array
     * @throws Exception
     */
    function sendMailRemindInterviewer($remindtype, $emails, $name, $subName, $projectName, $roomName, $url, $interviewTime, $authorizeCode, $rpoName, $rpoPhone, $isTemp) {
        try {
            //出现错误时是否直接抛出异常，如果抛出异常。另外需要异常捕捉程序，默认不开启。
            //$mail = new Mailer(true);
            $mail = new MailerService();
            //！！核心部分，指定通过无忧邮件平台发送
            $mail->isJobsmail(true);
            //非必须，如果有多个收件人，是一起批量发送还是独立发送多份邮件
            $mail->SingleTo = false;
            //后续所有邮箱的姓名均非必填
            //设置发件人邮箱及名字,非必须

            $mail->setFrom($mail->defaultSender['noreplay']['email'], $mail->defaultSender['noreplay']['name']);
            //添加收件人邮箱和姓名，至少要有一个收件人
            $emailArr = explode(';', $emails);
            $emailArr = array_unique($emailArr);
            if (count($emailArr) < 1) {
                return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => '收件人不可为空'];
            }
            foreach ($emailArr as $item) {
                if (empty($item)) {
                    continue;
                }
                $mail->addAddress($item);
            }

            //添加内置图片，需要一个唯一标识，便于文件定位
            $file_path = ROOT_PATH . "/Data/Image/logo.png";
            $cid = $mail->getUuid();
            $mail->addEmbeddedImage($file_path, $cid, 'logo.png');

            $file_path = ROOT_PATH . "/Data/Image/video_interviewer.jpg";
            $xcxid = $mail->getUuid();
            $mail->addEmbeddedImage($file_path, $xcxid, 'video_interviewer.jpg');

            //30分钟即将开始提醒
            $preTitle = $isTemp == 1 ? '[非正式面试]' : '';
            $mail->Subject = $preTitle . '【' . $subName . '/' . $projectName . '】在线面试提醒';
            $contentPart = '进行在线面试';
            if ($remindtype == 2) {
                $mail->Subject = $preTitle . '【' . $subName . '/' . $projectName . '】在线面试即将开始提醒';
                $contentPart = '进行的在线面试即将开始';
            }

            // Content
            $mail->isHTML(true);                                  // Set email format to HTML


            $file_path = ROOT_PATH . "/Data/Template/interview_video_1.html";
            if (!file_exists($file_path)) {
                return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => '模板不存在'];
            }

            //将整个文件内容读入到一个字符串中
            $emailBody = file_get_contents($file_path);
            $emailBody = str_replace("{{logo.png}}", "cid:" . $cid, $emailBody);
            $emailBody = str_replace("{{xcx.png}}", "cid:" . $xcxid, $emailBody);
            $emailBody = str_replace("{{url}}", $url, $emailBody);
            $emailBody = str_replace("{{name}}", $name, $emailBody);
            $emailBody = str_replace("{{contentPart}}", $contentPart, $emailBody);
            $emailBody = str_replace("{{interviewTime}}", $interviewTime, $emailBody);
            $emailBody = str_replace("{{authorizeCode}}", $authorizeCode, $emailBody);
            $emailBody = str_replace("{{rpoName}}", $rpoName, $emailBody);
            $emailBody = str_replace("{{rpoPhone}}", $rpoPhone, $emailBody);

            $mail->Body = $emailBody;

            $mail->setJobsmailFlag(config('mail.flag.cvreviewmail', 0));
            $mail->send();
        } catch (PHPMailerException $e) {
            return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => $e->getMessage()];
        }

        return ['status' => true, 'message' => '邮件发送成功'];
    }

    /**
     * Notes:面试官取消邮件
     * @param $emails
     * @param $name
     * @param $subName
     * @param $projectName
     * @param $roomName
     * @param $interviewTime
     * @param $rpoName
     * @param $rpoPhone
     * @param $isTemp
     * @return array
     * @throws Exception
     */
    function sendMailCancelRemindInterviewer($emails, $name, $subName, $projectName, $roomName, $interviewTime, $rpoName, $rpoPhone, $isTemp) {
        try {
            //出现错误时是否直接抛出异常，如果抛出异常。另外需要异常捕捉程序，默认不开启。
            //$mail = new Mailer(true);
            $mail = new MailerService();
            //！！核心部分，指定通过无忧邮件平台发送
            $mail->isJobsmail(true);
            //非必须，如果有多个收件人，是一起批量发送还是独立发送多份邮件
            $mail->SingleTo = false;
            //后续所有邮箱的姓名均非必填
            //设置发件人邮箱及名字,非必须

            $mail->setFrom($mail->defaultSender['noreplay']['email'], $mail->defaultSender['noreplay']['name']);
            //添加收件人邮箱和姓名，至少要有一个收件人
            $emailArr = explode(';', $emails);
            $emailArr = array_unique($emailArr);
            if (count($emailArr) < 1) {
                return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => '收件人不可为空'];
            }
            foreach ($emailArr as $item) {
                if (empty($item)) {
                    continue;
                }
                $mail->addAddress($item);
            }

            //添加内置图片，需要一个唯一标识，便于文件定位
            $file_path = ROOT_PATH . "/Data/Image/logo.png";
            $cid = $mail->getUuid();
            $mail->addEmbeddedImage($file_path, $cid, 'logo.png');

            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $preTitle = $isTemp == 1 ? '[非正式面试]' : '';
            $mail->Subject = $preTitle . '【' . $subName . '/' . $projectName . '】在线面试已取消';

            $file_path = ROOT_PATH . "/Data/Template/interview_video_2.html";
            if (!file_exists($file_path)) {
                return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => '模板不存在'];
            }

            //将整个文件内容读入到一个字符串中
            $emailBody = file_get_contents($file_path);
            $emailBody = str_replace("{{logo.png}}", "cid:" . $cid, $emailBody);
            $emailBody = str_replace("{{name}}", $name, $emailBody);
            $emailBody = str_replace("{{subName}}", $subName, $emailBody);
            $emailBody = str_replace("{{projectName}}", $projectName, $emailBody);
            $emailBody = str_replace("{{interviewTime}}", $interviewTime, $emailBody);
            $emailBody = str_replace("{{rpoName}}", $rpoName, $emailBody);
            $emailBody = str_replace("{{rpoPhone}}", $rpoPhone, $emailBody);

            $mail->Body = $emailBody;

            $mail->setJobsmailFlag(config('mail.flag.cvreviewmail', 0));
            $mail->send();
        } catch (PHPMailerException $e) {
            return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => $e->getMessage()];
        }

        return ['status' => true, 'message' => '邮件发送成功'];
    }


    /** 求职者提醒邮件
     * @param $remindtype
     * @param $emails
     * @param $subject
     * @param $name
     * @param $subName
     * @param $projectName
     * @param $caseName
     * @param $roomName
     * @param $url
     * @param $interviewTime
     * @param $authorizeCode
     * @param $rpoName
     * @param $rpoPhone
     * @return array
     * @throws Exception
     */
    function sendMailRemindJobseeker($remindtype, $emails, $name, $subName, $projectName, $caseName, $caseContent, $roomName, $url, $interviewTime, $authorizeCode, $rpoName, $rpoPhone, $isTemp) {
        try {
            //出现错误时是否直接抛出异常，如果抛出异常。另外需要异常捕捉程序，默认不开启。
            //$mail = new Mailer(true);
            $mail = new MailerService();
            //！！核心部分，指定通过无忧邮件平台发送
            $mail->isJobsmail(true);
            //非必须，如果有多个收件人，是一起批量发送还是独立发送多份邮件
            $mail->SingleTo = false;
            //后续所有邮箱的姓名均非必填
            //设置发件人邮箱及名字,非必须

            $mail->setFrom($mail->defaultSender['noreplay']['email'], $mail->defaultSender['noreplay']['name']);
            //添加收件人邮箱和姓名，至少要有一个收件人
            $emailArr = explode(';', $emails);
            $emailArr = array_unique($emailArr);
            if (count($emailArr) < 1) {
                return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => '收件人不可为空'];
            }
            foreach ($emailArr as $item) {
                if (empty($item)) {
                    continue;
                }
                $mail->addAddress($item);
            }

            //添加内置图片，需要一个唯一标识，便于文件定位
            $file_path = ROOT_PATH . "/Data/Image/logo.png";
            $cid = $mail->getUuid();
            $mail->addEmbeddedImage($file_path, $cid, 'logo.png');

            $file_path = ROOT_PATH . "/Data/Image/video_jobseeker.jpg";
            $xcxid = $mail->getUuid();
            $mail->addEmbeddedImage($file_path, $xcxid, 'video_jobseeker.jpg');

            //30分钟即将开始提醒
            $preTitle = $isTemp == 1 ? '[非正式面试]' : '';
            $mail->Subject = $preTitle . '【' . $subName . '/' . $caseName . '】在线面试提醒';
            $contentPart = '进行在线面试';
            if ($remindtype == 2) {
                $mail->Subject = $preTitle . '【' . $subName . '/' . $caseName . '】在线面试即将开始提醒';
                $contentPart = '进行的在线面试即将开始';
            }
            // Content
            $mail->isHTML(true);                                  // Set email format to HTML

            $file_path = ROOT_PATH . "/Data/Template/interview_video_3.html";
            if (!file_exists($file_path)) {
                return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => '模板不存在'];
            }

            //将整个文件内容读入到一个字符串中
            $emailBody = file_get_contents($file_path);
            $emailBody = str_replace("{{logo.png}}", "cid:" . $cid, $emailBody);
            $emailBody = str_replace("{{xcx.png}}", "cid:" . $xcxid, $emailBody);
            $emailBody = str_replace("{{url}}", $url, $emailBody);
            $emailBody = str_replace("{{name}}", $name, $emailBody);
            $emailBody = str_replace("{{contentPart}}", $contentPart, $emailBody);
            $emailBody = str_replace("{{interviewTime}}", $interviewTime, $emailBody);
            $emailBody = str_replace("{{authorizeCode}}", $authorizeCode, $emailBody);
            $emailBody = str_replace("{{subName}}", $subName, $emailBody);
            $emailBody = str_replace("{{caseName}}", $caseName, $emailBody);
            $emailBody = str_replace("{{caseContent}}", $caseContent, $emailBody);
            $emailBody = str_replace("{{rpoName}}", $rpoName, $emailBody);
            $emailBody = str_replace("{{rpoPhone}}", $rpoPhone, $emailBody);

            $mail->Body = $emailBody;

            $mail->setJobsmailFlag(config('mail.flag.cvreviewmail', 0));
            $mail->send();
        } catch (PHPMailerException $e) {
            return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => $e->getMessage()];
        }

        return ['status' => true, 'message' => '邮件发送成功'];
    }

    /**
     * Notes:求职者取消邮件
     * @param $emails
     * @param $name
     * @param $subName
     * @param $projectName
     * @param $caseName
     * @param $roomName
     * @param $interviewTime
     * @param $rpoName
     * @param $rpoPhone
     * @param $isTemp
     * @return array
     * @throws Exception
     */
    function sendMailCancelRemindJobseeker($emails, $name, $subName, $projectName, $caseName, $roomName, $interviewTime, $rpoName, $rpoPhone, $isTemp) {
        try {
            //出现错误时是否直接抛出异常，如果抛出异常。另外需要异常捕捉程序，默认不开启。
            //$mail = new Mailer(true);
            $mail = new MailerService();
            //！！核心部分，指定通过无忧邮件平台发送
            $mail->isJobsmail(true);
            //非必须，如果有多个收件人，是一起批量发送还是独立发送多份邮件
            $mail->SingleTo = false;
            //后续所有邮箱的姓名均非必填
            //设置发件人邮箱及名字,非必须

            $mail->setFrom($mail->defaultSender['noreplay']['email'], $mail->defaultSender['noreplay']['name']);
            //添加收件人邮箱和姓名，至少要有一个收件人
            $emailArr = explode(';', $emails);
            $emailArr = array_unique($emailArr);
            if (count($emailArr) < 1) {
                return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => '收件人不可为空'];
            }
            foreach ($emailArr as $item) {
                if (empty($item)) {
                    continue;
                }
                $mail->addAddress($item);
            }

            //添加内置图片，需要一个唯一标识，便于文件定位
            $file_path = ROOT_PATH . "/Data/Image/logo.png";
            $cid = $mail->getUuid();
            $mail->addEmbeddedImage($file_path, $cid, 'logo.png');

            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $preTitle = $isTemp == 1 ? '[非正式面试]' : '';
            $mail->Subject = $preTitle . '【' . $subName . '/' . $caseName . '】在线面试已取消';

            $file_path = ROOT_PATH . "/Data/Template/interview_video_4.html";
            if (!file_exists($file_path)) {
                return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => '模板不存在'];
            }

            //将整个文件内容读入到一个字符串中
            $emailBody = file_get_contents($file_path);
            $emailBody = str_replace("{{logo.png}}", "cid:" . $cid, $emailBody);
            $emailBody = str_replace("{{name}}", $name, $emailBody);
            $emailBody = str_replace("{{subName}}", $subName, $emailBody);
            $emailBody = str_replace("{{projectName}}", $projectName, $emailBody);
            $emailBody = str_replace("{{caseName}}", $caseName, $emailBody);
            $emailBody = str_replace("{{interviewTime}}", $interviewTime, $emailBody);
            $emailBody = str_replace("{{rpoName}}", $rpoName, $emailBody);
            $emailBody = str_replace("{{rpoPhone}}", $rpoPhone, $emailBody);

            $mail->Body = $emailBody;

            $mail->setJobsmailFlag(config('mail.flag.cvreviewmail', 0));
            $mail->send();
        } catch (PHPMailerException $e) {
            return ['status' => false, 'message' => '邮件发送失败', 'errorMsg' => $e->getMessage()];
        }

        return ['status' => true, 'message' => '邮件发送成功'];
    }
}